package com.decent.ejfadmin.collectcard.dao;

import com.decent.ejfadmin.collectcard.dao.provider.CollectCardDetailDaoProvider;
import com.decent.ejfadmin.collectcard.entity.ManualClearing;
import com.decent.ejfadmin.collectcard.entity.SupplyCardQueryBean;
import com.decent.ejfadmin.collectcard.response.CollectCardDetailDto;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.SupUser;
import com.decent.ejfadmin.common.bean.SupplyCard;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * 收卡明细查询
 *
 * @author weicx
 * @date 2018/07/20
 */
@Mapper
@Repository
public interface CollectCardDetailDao {
    /**
     * 收卡明细查询计数
     *
     * @param ids       会员id
     * @param startTime 查询开始时间
     * @param endTime   查询结束时间
     * @param card      查询bean
     * @param timeType  时间类型 0入库时间 1销卡时间
     * @return 总数
     */
    @SuppressWarnings("JavadocReference")
    @SelectProvider(type = CollectCardDetailDaoProvider.class, method = "queryReceiveListCount")
    int queryReceiveListCount(@Param("su") SupplyCardQueryBean supplyCardQueryBean, @Param("card") SupplyCard card);

    /**
     * 收卡明细查询分页
     *
     * @param pageUnit  分页组件
     * @param ids       会员id
     * @param startTime 查询开始时间
     * @param endTime   查询结束时间
     * @param card      查询bean
     * @param timeType  时间类型 0入库时间 1销卡时间
     * @param page      分页组件
     * @return list
     */
    @SuppressWarnings("JavadocReference")
    @SelectProvider(type = CollectCardDetailDaoProvider.class, method = "queryReceiveListPage")
    List<CollectCardDetailDto> queryReceiveListPage(@Param("su") SupplyCardQueryBean supplyCardQueryBean, @Param("card") SupplyCard card, @Param("page") Page page);

    /**
     * 按照内置id、批次号查询预判面值不符的收卡订单
     *
     * @param ids      内置id
     * @param batchIds 批次号
     * @return list
     */
    @SelectProvider(type = CollectCardDetailDaoProvider.class, method = "queryWrongFaceCard")
    List<SupplyCard> queryWrongFaceCard(@Param("ids") String ids, @Param("batchIds") String batchIds);

    /**
     * 预判不符卡密再次提交
     *
     * @param supplyCard 修改信息
     * @return 修改行数
     */
    @Update("update yuhuang_supply_card set status = #{card.status},endTime = #{card.endTime}," +
            "judgeFace = #{card.judgeFace} where orderId = #{card.orderId} and status = 5")
    int recommitCard(@Param("card") SupplyCard supplyCard);

    /**
     * 根据订单号查询回调地址
     *
     * @param orderId 订单ID
     * @return 回调地址
     */
    @Select("SELECT " +
            " u.api_type apiType, " +
            " u.api_state apiState, " +
            " c.call_back_url," +
            " u.notify_url notifyUrl " +
            "FROM " +
            " yuhuang_supply_card c, " +
            " yuhuang_sup_user u " +
            "WHERE " +
            " c.userId = u.user_id " +
            "AND c.orderId = #{orderId} ")
    SupUser queryCardCallBackAddress(@Param("orderId") String orderId);

    /**
     * 权益订单可售、纠纷中置失败
     *
     * @param id     订单id
     * @param remark 备注
     * @return
     */
    @Update("update yuhuang_supply_card set endTime=now(),failReason=#{remark},status=2 where  productType>310 and status=#{status} and id=#{id}")
    int equitySetFail(@Param("id") Long id, @Param("remark") String remark, @Param("status") Integer status);

    /**
     * 根据id查询订单
     *
     * @param id 订单id
     * @return
     */
    @Select("SELECT id, orderId, userId, batchId, cardNumber, cardPwd, productType, area, face, userPrice, settlePrice,\n" +
            " `status`, startTime, endTime, effectTime, startDate, added, failReason, judgeFace, supplyArea, rebate, \n" +
            " agentNo, mobileNo, cost, cardSource, speed, asynStatus, asynTime, call_back_url, message_push, productId,\n" +
            " aliKey FROM yuhuang_supply_card where id=#{id}")
    SupplyCard querySupplyOrderById(@Param("id") Long id);

    /**
     * 权益订单成功置失败
     *
     * @param id     订单id
     * @param remark 备注
     * @return
     */
    @Update("update yuhuang_supply_card set status=2,added = 2,endTime=now(),failReason=#{remark} where status =1 and productType>310 and id=#{id}")
    int equitySuccessToFail(@Param("id") Long id, @Param("remark") String remark);


    /**
     * 在售、待结算、纠纷中权益订单置成功
     *
     * @param id 订单id
     * @return
     */
    @Update("update yuhuang_supply_card set status=1,added = 2,endTime=now() where status in(0,9,10) and productType>310 and id=#{id}")
    int equitySetSuccess(@Param("id") Long id);

    /**
     * 修改会员权益信用分
     *
     * @param userId       会员id
     * @param equityCredit 权益信用分
     * @return
     */
    @Update("UPDATE yuhuang_sup_user SET equity_credit=equity_credit+#{equityCredit} WHERE user_id=#{userId}")
    int modifySupUserEquityCredit(@Param("userId") Long userId, @Param("equityCredit") BigDecimal equityCredit);


    /**
     * 更新会员备注【停售原因】
     *
     * @param id
     * @param remark
     * @return
     */
    @Update("update yuhuang_supply_card set failReason=#{remark} where id=#{id}")
    int updateRemark(@Param("id") Long id, @Param("remark") String remark);

    /**
     * 更新运营备注【停售原因】
     *
     * @param id
     * @param remark
     * @return
     */
    @Update("update yuhuang_supply_card set remark=CONCAT_WS('|',remark,#{remark}) where id=#{id}")
    int updateAdminRemark(@Param("id") Long id, @Param("remark") String remark);

    /**
     * 根据Id获取收卡明细订单数据
     *
     * @param id
     * @return
     */
    @Select("select * from yuhuang_supply_card where id=#{id}")
    SupplyCard getSupplyCardOrderById(@Param("id") Long id);

    /**
     * 根据订单号获取收卡明细订单数据
     *
     * @param orderId
     * @return
     */
    @Select("select * from yuhuang_supply_card where orderId=#{orderId}")
    SupplyCard getSupplyCardOrderByOrderId(@Param("orderId") String orderId);


    /**
     * 标记手动补结算转账记录
     *
     * @param orderId
     * @param remark
     * @return
     */
    @Update("update yuhuang_supply_card set transfer_mark=1,remark=CONCAT_WS('|',remark,#{remark}) where orderId=#{orderId}")
    int updateTransferMarkByManualClearing(@Param("orderId") String orderId, @Param("remark") String remark);

    /**
     * 补结算订单记录
     *
     * @param manualClearing
     * @return
     */
    @Insert("insert into yuhuang_manual_clearing_order " +
            "(batch_id," +
            " order_id," +
            "transfer_price," +
            "user_id," +
            "start_time)" +
            "values(" +
            "#{manualClearing.batchId}," +
            "#{manualClearing.orderId}," +
            "#{manualClearing.addUserPrice}," +
            "#{manualClearing.userId}," +
            "now())")
    int insertManualClearing(@Param("manualClearing") ManualClearing manualClearing);

    /**
     * 更新补结算订单信息
     *
     * @param manualClearing
     * @return
     */
    @Update(" update yuhuang_manual_clearing_order set" +
            " admin=#{manualClearing.admin}," +
            " status=#{manualClearing.status}," +
            " end_time=now()," +
            " remark=#{manualClearing.remark}," +
            " apply_type=#{manualClearing.applyType}," +
            " zfb_order_id=#{manualClearing.zfbOrderId} " +
            "  where order_id=#{manualClearing.orderId}")
    int updateManualClearing(@Param("manualClearing") ManualClearing manualClearing);

    /**
     * 获取补结算订单
     *
     * @param orderId
     * @return
     */
    @Select("select id," +
            "status," +
            "batch_id batchId," +
            "order_id orderId," +
            "transfer_price addUserPrice," +
            "apply_type applyType," +
            "user_id userId," +
            "admin " +
            "from yuhuang_manual_clearing_order where order_id=#{orderId}")
    ManualClearing getManualClearing(@Param("orderId") String orderId);

    /**
     * 获取需要再次提交的权益订单
     *
     * @param ids
     * @return
     */
    @Select("select * from yuhuang_supply_card where productType>310 and id in (${ids}) and status in(3,0)")
    List<SupplyCard> equityBatchOrderList(@Param("ids") String ids);

    /**
     * 更新批次再次提交标记
     *
     * @param orderId
     * @param remark
     * @param newStatus
     * @param oldStatus
     * @return
     */
    @Update("update yuhuang_supply_card set resubmission_mark = 1," +
            "status = #{newStatus}," +
            "remark = CONCAT_WS('|', remark, #{remark}) " +
            "where orderId = #{orderId} and status = #{oldStatus} and (resubmission_mark = 0 or resubmission_mark is null)")
    int updateBatchResubmissionMark(@Param("orderId") String orderId, @Param("remark") String remark,
                                    @Param("newStatus") Integer newStatus, @Param("oldStatus") Integer oldStatus);

    /**
     * 查询批次是否已经提交过
     *
     * @param orderId 订单号
     * @return 再次提交记录数
     */
    @Select("select count(*) from yuhuang_supply_card where orderId = #{orderId} and resubmission_mark = 1")
    int queryResubmissionMark(@Param("orderId") String orderId);
}
